import {
  get,
  post,
  del,
  put
} from "./base_service"

/**
 * 房源相关接口
 * 1.查询房源
 * 2.搜索房源
 * 3.楼盘对比
 * 
 * allen  2021-08-02
 */


/**
 * 根据tag标签查询
 * @param {*} page 
 * @param {*} pageSize 
 * @param {*} tag 
 */
const queryHouseByTag = (page, pageSize, tag) => {
  return new Promise((resolve, reject) => {
    get("/new/house/query/tag?page=" + page + "&pageSize=" + pageSize + "&tag=" + tag).then((data) => {
      if (data.code == 200) {
        resolve(data)
      } else {
        reject(data.msg)
      }
    }).catch((e) => {
      reject(e.message)
    })
  })
}

/**
 * 查询发布的房源(当前新房)
 * @param {*} page 
 * @param {*} pageSize 
 */
const queryMyHouseResource = (page, pageSize, words, examineStatus) => {
  return new Promise((resolve, reject) => {
    get("/new/house/manager/list?page=" + page + "&pageSize=" + pageSize + "&words=" + words + "&examineStatus=" + examineStatus).then((data) => {
      if (data.code == 200) {
        resolve(data)
      } else {
        reject(data.msg)
      }
    }).catch((e) => {
      reject(e.message)
    })
  })
}

/**
 * 根据房源number查询房源信息
 * @param {*} houseNumber 
 */
const queryNewHouseDetail = (houseNumber) => {
  return new Promise((resolve, reject) => {
    get("/new/house/base/detail?houseNumber=" + houseNumber).then((data) => {
      if (data.code == 200) {
        resolve(data)
      } else {
        reject(data.msg)
      }
    }).catch((e) => {
      reject(e.message)
    })
  })
}

/**
 * 根据房源number查询房源其他信息
 * @param {*} houseNumber 
 */
const queryNewHouseOtherDetail = (houseNumber) => {
  return new Promise((resolve, reject) => {
    get("/new/house/other/detail?houseNumber=" + houseNumber).then((data) => {
      if (data.code == 200) {
        resolve(data)
      } else {
        reject(data.msg)
      }
    }).catch((e) => {
      reject(e.message)
    })
  })
}


/**
 * 根据房源标签查询
 * @param {*} houseNumber 
 * 1:经纪人 2:驻场 3:租房，4。二手
 */
const queryNewHouseExpert = (houseNumber, type) => {
  return new Promise((resolve, reject) => {
    get("/expert/house?houseNumber=" + houseNumber + "&type=" + type).then((data) => {
      if (data.code == 200) {
        resolve(data)
      } else {
        reject(data.msg)
      }
    }).catch((e) => {
      reject(e.message)
    })
  })
}


/**
 * 查询楼盘的评论列表
 * @param {*} houseNumber 
 */
const queryNewHouseComment = (houseNumber) => {
  return new Promise((resolve, reject) => {
    get("/comment/house?houseNumber=" + houseNumber).then((data) => {
      if (data.code == 200) {
        resolve(data)
      } else {
        reject(data.msg)
      }
    }).catch((e) => {
      reject(e.message)
    })
  })
}

/**
 * 给评论点赞
 * @param {*} id 
 */
const approveComment = (id) => {
  return new Promise((resolve, reject) => {
    get("/comment/approve?id=" + id).then((data) => {
      if (data.code == 200) {
        resolve(data)
      } else {
        reject(data.msg)
      }
    }).catch((e) => {
      reject(e.message)
    })
  })
}

/**
 * 创建一条评论
 * @param {*} data 
 */
const createComment = (data) => {
  return new Promise((resolve, reject) => {
    post("/dyn/comments/create", data).then((data) => {
      if (data.code == 200) {
        resolve(data)
      } else {
        reject(data.msg)
      }
    }).catch((e) => {
      reject(e.message)
    })
  })
}

/**
 * 删除一条评论
 * @param {*} id 
 * @param {*} houseNumber 
 */
const deleteComment = (id, dynId) => {
  return new Promise((resolve, reject) => {
    get("/dyn/comments/del?dynId=" + dynId + "&id=" + id).then((data) => {
      if (data.code == 200) {
        resolve(data)
      } else {
        reject(data.msg)
      }
    }).catch((e) => {
      reject(e.message)
    })
  })
}


/**
 * 根据条件删选新房
 * @param {*} data 
 */
const queryNewHouseByCondition = (data) => {
  console.log(data);
  return new Promise((resolve, reject) => {
    post("/new/house/search/query", data).then((data) => {
      if (data.code == 200) {
        resolve(data)
      } else {
        reject(data.msg)
      }
    }).catch((e) => {
      reject(e.message)
    })
  })
}


/**
 * 报备查询的推荐房源
 * @param {*} data 
 */
const queryRecommendHouse = (data) => {
  return new Promise((resolve, reject) => {
    post("/new/house/query/all/sample", data).then((data) => {
      if (data.code == 200) {
        resolve(data)
      } else {
        reject(data.msg)
      }
    }).catch((e) => {
      reject(e.message)
    })
  })
}

/**
 * 楼盘对比列表
 */
const queryCompareList = () => {
  return new Promise((resolve, reject) => {
    get("/new/house/query/compare/list").then((data) => {
      if (data.code == 200) {
        resolve(data)
      } else {
        reject(data.msg)
      }
    }).catch((e) => {
      reject(e.message)
    })
  })
}

/**
 * 添加一个房源去对比列表
 * @param {*} houseNumber 
 */
const addHouseCompare = (houseNumber) => {
  return new Promise((resolve, reject) => {
    get("/new/house/query/compare/house", {
      houseNumber: houseNumber
    }).then((data) => {
      if (data.code == 200) {
        resolve(data)
      } else {
        reject(data.msg)
      }
    }).catch((e) => {
      reject(e.message)
    })
  })
}

/**
 * 楼盘对比详情
 * @param {*} houseNumber1 
 * @param {*} houseNumber2 
 */
const queryHouseCompareDetail = (houseNumber1, houseNumber2) => {
  return new Promise((resolve, reject) => {
    get("/new/house/query/compare/detail", {
      houseNumber1: houseNumber1,
      houseNumber2: houseNumber2
    }).then((data) => {
      if (data.code == 200) {
        resolve(data)
      } else {
        reject(data.msg)
      }
    }).catch((e) => {
      reject(e.message)
    })
  })
}


/**
 * 查询房源热议榜
 * @param {*} cityName 
 * @param {*} page 
 * @param {*} pageSize 
 * @param {*} words 
 */
const queryHouseCommentList = (cityName, page, pageSize, words) => {
  return new Promise((resolve, reject) => {
    get("/new/house/comment/list", {
      cityName,
      page,
      pageSize,
      words
    }).then((data) => {
      if (data.code == 200) {
        resolve(data)
      } else {
        reject(data.msg)
      }
    }).catch((e) => {
      reject(e.message)
    })
  })
}
/**
 * 查询房源人气榜
 * @param {*} cityName 
 * @param {*} page 
 * @param {*} pageSize 
 * @param {*} words 
 */
const queryHousePeopleList = (cityName, page, pageSize, words) => {
  return new Promise((resolve, reject) => {
    get("/new/house/people/list", {
      cityName,
      page,
      pageSize,
      words
    }).then((data) => {
      if (data.code == 200) {
        resolve(data)
      } else {
        reject(data.msg)
      }
    }).catch((e) => {
      reject(e.message)
    })
  })
}

/**
 * 查询房源销量榜
 * @param {*} cityName 
 * @param {*} page 
 * @param {*} pageSize 
 * @param {*} words 
 */
const queryHouseSellList = (cityName, page, pageSize, words) => {
  return new Promise((resolve, reject) => {
    get("/new/house/sell/list", {
      cityName,
      page,
      pageSize,
      words
    }).then((data) => {
      if (data.code == 200) {
        resolve(data)
      } else {
        reject(data.msg)
      }
    }).catch((e) => {
      reject(e.message)
    })
  })
}

/**
 * 搜索时查询推荐的房源标签
 */
const querySearchRecommendTag = () => {
  return new Promise((resolve, reject) => {
    get("/search/recommend").then((data) => {
      if (data.code == 200) {
        resolve(data)
      } else {
        reject(data.msg)
      }
    }).catch((e) => {
      reject(e.message)
    })
  })
}

/**
 * 传参查询地图找房信息
 * @param {*} data 
 */
const searchHouseMapAction = (data) => {
  return new Promise((resolve, reject) => {
    post('/common/map/houme', data).then((data) => {
      if (data.code == 200) {
        resolve(data)
      } else {
        reject(data.msg)
      }
    }).
      catch((e) => {
        wx.showToast({
          icon: "none",
          title: '网络异常稍后再试',
        })
      })
  })
}


/**
 * 提交ai找房
 * @param {*} data 
 */
const submitAiFindHouse = (data) => {
  return new Promise((resolve, reject) => {
    post('/new/house/ai/find', data).then((data) => {
      if (data.code == 200) {
        resolve(data)
      } else {
        reject(data.msg)
      }
    }).
      catch((e) => {
        wx.showToast({
          icon: "none",
          title: '网络异常稍后再试',
        })
      })
  })
}

//地图找房相关=======

/**
 * 根据城市查询城市辖区内的区县房源个数和区县定位
 * @param {*} cityName 
 */
const queryMapRegionRequest = (cityName) => {
  return new Promise((resolve, reject) => {
    get('/new/house/statistics/map/find?cityName=' + cityName).then((data) => {
      if (data.code == 200) {
        resolve(data)
      } else {
        reject(data.msg)
      }
    }).
      catch((e) => {
        wx.showToast({
          icon: "none",
          title: '网络异常稍后再试',
        })
      })
  })
}





//地图找房相关=======

/**
 * 提交房源去审核
 * @param {*} houseNumber 
 */
const submitExamineHouseRequest = (houseNumber) => {
  return new Promise((resolve, reject) => {
    get('/new/house/manager/submit/examine?houseNumber=' + houseNumber).then((data) => {
      if (data.code == 200) {
        resolve(data)
      } else {
        reject(data.msg)
      }
    }).
      catch((e) => {
        wx.showToast({
          icon: "none",
          title: '网络异常稍后再试',
        })
      })
  })
}

/**
 * 房源的上架和下架接口
 * @param {*} houseNumber 
 */
const onOrOffHouse = (houseNumber) => {
  return new Promise((resolve, reject) => {
    put('/new/house/manager/push?houseNumber=' + houseNumber).then((data) => {
      if (data.code == 200) {
        resolve(data)
      } else {
        reject(data.msg)
      }
    }).catch((e) => {
      wx.showToast({
        icon: "none",
        title: '网络异常稍后再试',
      })
    })
  })
}

/**
 * 删除一个房源
 * @param {*} houseNumber 
 */
const deleteOneHouse = (houseNumber) => {
  return new Promise((resolve, reject) => {
    del('/new/house/manager/delete?houseNumber=' + houseNumber).then((data) => {
      if (data.code == 200) {
        resolve(data)
      } else {
        reject(data.msg)
      }
    }).catch((e) => {
      wx.showToast({
        icon: "none",
        title: '网络异常稍后再试',
      })
    })
  })
}

/**
 * 删除一个租房房源
 * @param {*} houseNumber 
 */
const deleteRentOneHouse = (houseNumber) => {
  return new Promise((resolve, reject) => {
    del('/rent/house/manager/delete?houseNumber=' + houseNumber).then((data) => {
      if (data.code == 200) {
        resolve(data)
      } else {
        reject(data.msg)
      }
    }).catch((e) => {
      wx.showToast({
        icon: "none",
        title: '网络异常稍后再试',
      })
    })
  })
}

/**
 * 预览房源获取详情
 * @param {*} houseNumber 
 */
const queryPreviewHouseDetail = (houseNumber) => {
  return new Promise((resolve, reject) => {
    get('/new/house/manager/base/detail?houseNumber=' + houseNumber).then((data) => {
      if (data.code == 200) {
        resolve(data)
      } else {
        reject(data.msg)
      }
    }).catch((e) => {
      wx.showToast({
        icon: "none",
        title: '网络异常稍后再试',
      })
    })
  })
}

/**
 * 管理员审核新房
 * @param {*} houseNumber 
 * @param {*} isPass 
 * @param {*} remark 
 */
const managerCheckNewHouse = (houseNumber, isPass, remark) => {
  return new Promise((resolve, reject) => {
    get('/new/house/manager/manager/examine?houseNumber=' + houseNumber + "&isPass=" + isPass + "&remark=" + remark).then((data) => {
      if (data.code == 200) {
        resolve(data)
      } else {
        reject(data.msg)
      }
    }).catch((e) => {
      wx.showToast({
        icon: "none",
        title: '网络异常稍后再试',
      })
    })
  })
}

/**
 * 查询新房标签
 * type: 0 :普通标签
 *       1: 房源主标签
 */
const queryNewHouseTags = (type, words = "") => {
  return new Promise((resolve, reject) => {
    get('/house/tag?type=' + type + "&words=" + words).then((data) => {
      if (data.code == 200) {
        resolve(data)
      } else {
        reject(data.msg)
      }
    }).catch((e) => {
      wx.showToast({
        icon: "none",
        title: '网络异常稍后再试',
      })
    })
  })
}

/**
 * 根据房源编号查询楼盘总平
 * @param {*} houseNumber 
 */
const queryHouseTotalFlat = (houseNumber) => {
  return new Promise((resolve, reject) => {
    get('/new/house/query/total/flat?houseNumber=' + houseNumber).then((data) => {
      if (data.code == 200) {
        resolve(data)
      } else {
        reject(data.msg)
      }
    }).catch((e) => {
      wx.showToast({
        icon: "none",
        title: '网络异常稍后再试',
      })
    })
  })
}

/**
 * 查询户型详情
 * @param {*} formatNumber 
 */
const queryFormatDetail = (formatNumber) => {
  return new Promise((resolve, reject) => {
    get('/new/house/query/format/detail?formatNumber=' + formatNumber).then((data) => {
      if (data.code == 200) {
        resolve(data)
      } else {
        reject(data.msg)
      }
    }).catch((e) => {
      wx.showToast({
        icon: "none",
        title: '网络异常稍后再试',
      })
    })
  })
}

/**
 * 查询推荐户型
 * @param {*} formatNumber 
 */
const queryRecommendFormat = (formatNumber) => {
  return new Promise((resolve, reject) => {
    get('/new/house/query/format/recommend?formatNumber=' + formatNumber).then((data) => {
      if (data.code == 200) {
        resolve(data)
      } else {
        reject(data.msg)
      }
    }).catch((e) => {
      wx.showToast({
        icon: "none",
        title: '网络异常稍后再试',
      })
    })
  })
}

/**
 * 查询佣金详情
 * @param {*} id 
 */
const queryHouseCommissionDetail = (id) => {
  return new Promise((resolve, reject) => {
    get('/commission/detail?id=' + id).then((data) => {
      if (data.code == 200) {
        resolve(data)
      } else {
        reject(data.msg)
      }
    }).catch((e) => {
      wx.showToast({
        icon: "none",
        title: '网络异常稍后再试',
      })
    })
  })
}

/**
 * 查询房源二维码
 * @param {*} houseNumber 
 */
const queryHouseQrCode = (houseNumber, houseType) => {
  return new Promise((resolve, reject) => {
    get('/qr/code/create/qr/code?houseNumber=' + houseNumber + "&houseType=" + houseType).then((data) => {
      if (data.code == 200) {
        resolve(data)
      } else {
        reject(data.msg)
      }
    }).catch((e) => {
      wx.showToast({
        icon: "none",
        title: '网络异常稍后再试',
      })
    })
  })
}

/**
 * 设置新房详情的变价提醒和开盘提醒
 * type:1 : 变价提醒  2: 开盘提醒
 * @param {*} houseNumber 
 * @param {*} type 
 */
const settingHouseRemind = (houseNumber, type, formatNumber = "") => {
  return new Promise((resolve, reject) => {
    get('/new/house/remind?houseNumber=' + houseNumber + "&type=" + type + "&formatNumber=" + formatNumber).then((data) => {
      if (data.code == 200) {
        resolve(data)
      } else {
        reject(data.msg)
      }
    }).catch((e) => {
      wx.showToast({
        icon: "none",
        title: '网络异常稍后再试',
      })
    })
  })
}


/**
 * 发布新房动态
 * @param {*} data 
 */
const publishNewHouseDyn = (data) => {
  return new Promise((resolve, reject) => {
    post('/new/house/manager/create/dyn', data).then((data) => {
      if (data.code == 200) {
        resolve(data)
      } else {
        reject(data.msg)
      }
    }).catch((e) => {
      wx.showToast({
        icon: "none",
        title: '网络异常稍后再试',
      })
    })
  })
}

/**
 * 删除一个房源动态
 * @param {*} id 
 */
const deleteNewHouseDyn = (id) => {
  return new Promise((resolve, reject) => {
    del('/new/house/manager/delete/dyn?id=' + id).then((data) => {
      if (data.code == 200) {
        resolve(data)
      } else {
        reject(data.msg)
      }
    }).catch((e) => {
      wx.showToast({
        icon: "none",
        title: '网络异常稍后再试',
      })
    })
  })
}

/**
 * 房源详情的拨打电话
 * @param {*} id 
 * @param {*} houseNumber 
 */
const expertPhoneAction = (id, houseNumber) => {
  return new Promise((resolve, reject) => {
    get('/expert/phone?id=' + id + "&houseNumber=" + houseNumber).then((data) => {
      if (data.code == 200) {
        resolve(data)
      } else {
        reject(data.msg)
      }
    }).catch((e) => {
      wx.showToast({
        icon: "none",
        title: '网络异常稍后再试',
      })
    })
  })
}


/**
 * 关闭或者开启房源报备状态
 * @param {*} houseNumber 
 */
const settingReportStatus = (houseNumber) => {
  return new Promise((resolve, reject) => {
    get('/new/house/manager/report/setting?houseNumber=' + houseNumber).then((data) => {
      if (data.code == 200) {
        resolve(data)
      } else {
        reject(data.msg)
      }
    }).catch((e) => {
      wx.showToast({
        icon: "none",
        title: '网络异常稍后再试',
      })
    })
  })
}
/**
 * 查询房源是否已经存在
 * @param {*} name 
 */
const queryNewHouseIsExit = (name) => {
  return new Promise((resolve, reject) => {
    get('/new/house/manager/isExit?name=' + name).then((data) => {
      if (data.code == 200) {
        resolve(data)
      } else {
        reject(data.msg)
      }
    }).catch((e) => {
      wx.showToast({
        icon: "none",
        title: '网络异常稍后再试',
      })
    })
  })
}

//单独的搜索房源接口:
/**
 * 单独的搜索房源接口
 * @param {*} houseType 
 * @param {*} words 
 */
const searchHouseByWords = (houseType, words) => {
  return new Promise((resolve, reject) => {
    get('/search/word?houseType=' + houseType + "&words=" + words).then((data) => {
      if (data.code == 200) {
        resolve(data)
      } else {
        reject(data.msg)
      }
    }).catch((e) => {
      wx.showToast({
        icon: "none",
        title: '网络异常稍后再试',
      })
    })
  })
}

/**
 * 新房顶部数量
 */
const queryNewHouseNumbers = () => {
  return new Promise((resolve, reject) => {
    get('/new/house/manager/examine/house/number').then((data) => {
      if (data.code == 200) {
        resolve(data)
      } else {
        reject(data.msg)
      }
    }).catch((e) => {
      wx.showToast({
        icon: "none",
        title: '网络异常稍后再试',
      })
    })
  })
}


/**
 * 根据id获取video详情
 * @param {*} id 
 */
const queryVideoDetail = (id) => {
  return new Promise((resolve, reject) => {
    get('/live/query/detail?id=' + id).
      then((data) => {
        if (data.code == 200) {
          resolve(data)
        } else {
          reject(data.msg)
        }
      }).catch((e) => {
        reject(e.message)
      })
  })
}
























module.exports = {
  queryHouseByTag,
  queryMyHouseResource,
  queryNewHouseDetail,
  queryNewHouseOtherDetail,
  queryNewHouseExpert,
  queryNewHouseComment,
  approveComment,
  createComment,
  deleteComment,
  queryNewHouseByCondition,
  queryRecommendHouse,
  queryCompareList,
  addHouseCompare,
  queryHouseCompareDetail,
  queryHouseCommentList,
  queryHousePeopleList,
  queryHouseSellList,
  querySearchRecommendTag,
  searchHouseMapAction,
  submitAiFindHouse,
  queryMapRegionRequest,
  submitExamineHouseRequest,
  onOrOffHouse,
  deleteOneHouse,
  queryPreviewHouseDetail,
  managerCheckNewHouse,
  queryNewHouseTags,
  queryHouseTotalFlat,
  queryFormatDetail,
  queryRecommendFormat,
  queryHouseCommissionDetail,
  queryHouseQrCode,
  settingHouseRemind,
  publishNewHouseDyn,
  deleteNewHouseDyn,
  expertPhoneAction,
  settingReportStatus,
  queryNewHouseIsExit,
  searchHouseByWords,
  queryNewHouseNumbers,
  queryVideoDetail,
  deleteRentOneHouse
}

